##################################
###   Ambiguity project        ###
###   Jonne Kamphorst          ###
###   2022                     ###
###   Version: Dec &jan 22, 23 ###   
##################################


## Load libaries and data

library(tidyverse)
library(extrafont)
library(viridis)
library(texreg)
library(ggpubr)

load("data/final_exp2.Rda")



#font_import()
loadfonts(quiet = T, device = "pdf")
windowsFonts(Georgia = windowsFont("Georgia")) #load fonts for windows machines


## Set ggplot theme with the Georgia font
theme_set(theme_light(base_family = "Georgia", base_size = 12))







############# Data center experiment: clean background variables  -------------------
## edu dummy
data$uni_education <- ifelse(grepl("Bachelor", data$Education) |
                               grepl("Doctoraat", data$Education) |
                               grepl("universiteit", data$Education), 1, 0)

# continuous scale
data$edu_cont <- NA
data$edu_cont[grepl("Basissch", data$Education)]<- 0
data$edu_cont[grepl("VMBO", data$Education)]<- 1
data$edu_cont[grepl("KMBO", data$Education)]<- 2
data$edu_cont[grepl("niveau 1", data$Education)]<- 3
data$edu_cont[grepl("niveau 2", data$Education)]<- 4
data$edu_cont[grepl("HAVO jaar", data$Education)]<- 5
data$edu_cont[grepl("HAVO, MMS", data$Education)]<- 6
data$edu_cont[grepl("niveau 4", data$Education)]<- 7
data$edu_cont[grepl("VWO", data$Education)]<- 8
data$edu_cont[grepl("HBO", data$Education)]<- 9
data$edu_cont[grepl("Bachelor univ", data$Education)]<- 10
data$edu_cont[grepl("Master", data$Education)]<- 11
data$edu_cont[grepl("Doctoraat", data$Education)]<- 12






################### Data center experiment: main outcomes -------------------
##  Check whether treated people are better at picking the party that agrees with them
data$data_outcome_binar_a1 <- ifelse(data$data_outcome_binar=="Partij A", 1, 0)
mod <- (lm(data_outcome_binar_a1~treatment_data  + pos_data + Gender + edu_cont, data=data))
summary(mod)

##  The continuous outcome (difference between party A and B)
data$diffparta_partyb <- ((data$data_outcome_pa_1 +data$data_outcome_pa_2)/2) - 
  ((data$data_outcome_pb_1 + data$data_outcome_pb_2)/2)


mod2 <- (lm(diffparta_partyb~treatment_data   + pos_data + Gender + edu_cont, data=data))
summary(mod2)


## Create a table for in the appendix
table <- texreg(l=list(mod, mod2),
                use.packages=F,
                stars=c(0.10, 0.05, 0.01), 
                digits=3, include.ci = FALSE, booktabs=T,
                custom.model.names = c("Choosing the agreeing party", "Difference between parties"),
                caption.above=T,
                label="exp2_main",
                caption="Effect of salience treatment on ability to distinguish between parties",
                threeparttable = TRUE,
                float.pos="hptb!",
                custom.coef.names = c("Intercept", "Treated", "Position Disagree", "Female", "Education"),
                reorder.coef = c(2,3,4,5,1),
                custom.note="\\item %stars. The first model uses as outcome whether people choose the party that agrees with them (1 versus 0). The second model uses as outcome the difference between the party that agrees with a respondent and the party that is ambiguous on a composite scale that is composed of two continuous items (from 0 to 100). The composite ranges from -100 (completely preferring the ambiguous party) to 100 (completely preferring the agreeing party). Models are estimated through OLS")


# Table A4.
#print(table, file = "C:/Users/jonne/Dropbox/Apps/Overleaf/Too Important to Ignore v11 - resub JOP/tables/exp2_main.tex")
print(table, file = "tables/exp2_main.tex")







## Create two plots that you then put together. 
# outcome in the facet label at the top. The whiskers show 10% and 5% significance. 
resmod <- data.frame(coef=summary(mod)$coefficients[2],
                     SEs=summary(mod)$coefficients[7])
resmod$conf_high <- resmod$coef + 1.96 * resmod$SEs
resmod$conf_low <- resmod$coef - 1.96 * resmod$SEs
resmod$conf_high10 <- resmod$coef + 1.282 * resmod$SEs
resmod$conf_low10 <- resmod$coef - 1.282 * resmod$SEs
resmod$coefficient <- ""


plot1 <- ggplot(resmod) + 
  geom_pointrange(aes(x=coef, y=coefficient, xmin=conf_low, xmax=conf_high), size=0.9) +
  geom_linerange(aes(x=coef, y=coefficient, xmin=conf_low10, xmax=conf_high10), size=1.8) +
  geom_vline(xintercept = 0, linetype="dashed") +
  labs(y="", x="Treatment effect") + 
  facet_grid(~"DV: Choosing the 'agreeing' party (0 or 1)") +
  theme(axis.text=element_text(size=11), 
        legend.text=element_text(size=9), strip.background =element_rect(fill="lightgrey"), 
        strip.text = element_text(colour = 'black'),
        strip.text.x = element_text(size = 12)) 

resmod <- data.frame(coef=summary(mod2)$coefficients[2],
                     SEs=summary(mod2)$coefficients[7])
resmod$conf_high <- resmod$coef + 1.96 * resmod$SEs
resmod$conf_low <- resmod$coef - 1.96 * resmod$SEs
resmod$conf_high10 <- resmod$coef + 1.282 * resmod$SEs
resmod$conf_low10 <- resmod$coef - 1.282 * resmod$SEs
resmod$coefficient <- ""


plot2 <- ggplot(resmod) + 
  geom_pointrange(aes(x=coef, y=coefficient, xmin=conf_low, xmax=conf_high), size=0.9) +
  geom_linerange(aes(x=coef, y=coefficient, xmin=conf_low10, xmax=conf_high10), size=1.8) +
  geom_vline(xintercept = 0, linetype="dashed") +
  labs(y="", x="Treatment effect") + 
  facet_grid(~"DV: Difference between parties (-100 to 100)") +
  theme(axis.text=element_text(size=11), 
        legend.text=element_text(size=9), strip.background =element_rect(fill="lightgrey"), 
        strip.text = element_text(colour = 'black'),
        strip.text.x = element_text(size = 12)) 


# Add plots together and save
plots <- ggarrange(plot1, plot2, nrow=2, ncol=1)

# Save the plots (figure 7)
ggsave(plot=plots, "plots/fig7_exp2_main.png", dpi = 300, width=6.1, height=3.2)
ggsave(plot=plots, "plots/fig7_exp2_main.tiff", dpi = 300, width=6.1, height=3.2, device="tiff")
#ggsave(plot=plots, "C:/Users/jonne/Dropbox/Apps/Overleaf/Too Important to Ignore v11 - resub JOP/plots/exp2_main.png", dpi = 300, width=6.1, height=3.2)







################### Data center experiment: Manipulation outcomes & Mechanism outcomes -------------------
##### Importance, (three outcomes and composite)
data$import_comp <- (data$data_imp_1 + data$data_imp_9 + data$data_imp_7)/3

mod <- (lm(import_comp~treatment_data  + pos_data + Gender + edu_cont, data=data))
summary(mod)
mod1 <- (lm(data_imp_1~treatment_data  + pos_data + Gender + edu_cont, data=data))
summary(mod1)
mod2 <- (lm(data_imp_7~treatment_data  + pos_data + Gender + edu_cont, data=data))
summary(mod2)
mod3 <- (lm(data_imp_9~treatment_data  + pos_data + Gender + edu_cont, data=data))
summary(mod3)



## Create table
## Create a table for in the appendix
table <- texreg(l=list(mod, mod1, mod2, mod3),
                use.packages=F,
                stars=c(0.10, 0.05, 0.01), 
                digits=3, include.ci = FALSE, booktabs=T,
                custom.model.names = c("Composite", "Important subject", "Personal importance", "More important than before"),
                caption.above=T,
                label="exp2_manip",
                caption="Effect of salience treatment on importance of the issue",
                threeparttable = TRUE,
                float.pos="hptb!",
                custom.coef.names = c("Intercept", "Treated", "Position Disagree", "Female", "Education"),
                reorder.coef = c(2,3,4,5,1),
                custom.note="\\item %stars. The first model uses as outcome a composite of the other three importance scales. All outcomes range from 0 to 100. Models are estimated through OLS")

# Table A5
#print(table, file = "C:/Users/jonne/Dropbox/Apps/Overleaf/Too Important to Ignore v11 - resub JOP/tables/exp2_manip.tex")
print(table, file = "tables/exp2_manip.tex")



## Create plot
resmod <- data.frame(coef=c(summary(mod)$coefficients[2], summary(mod1)$coefficients[2], 
                            summary(mod2)$coefficients[2], 
                            summary(mod3)$coefficients[2]),
                     SEs=c(summary(mod)$coefficients[7], summary(mod1)$coefficients[7],
                           summary(mod2)$coefficients[7],
                           summary(mod3)$coefficients[7]),
                     outcome=c("Importance composite", "Important subject", "Personal importance", "More important than before"))



resmod$conf_high <- resmod$coef + 1.96 * resmod$SEs
resmod$conf_low <- resmod$coef - 1.96 * resmod$SEs
resmod$conf_high10 <- resmod$coef + 1.282 * resmod$SEs
resmod$conf_low10 <- resmod$coef - 1.282 * resmod$SEs


plot1 <- ggplot(resmod) + 
  geom_pointrange(aes(x=coef, y=outcome, xmin=conf_low, xmax=conf_high), size=0.9) +
  geom_linerange(aes(x=coef, y=outcome, xmin=conf_low10, xmax=conf_high10), size=1.8) +
  geom_vline(xintercept = 0, linetype="dashed") +
  labs(y="", x="Treatment effect") + 
  facet_grid(~"DVs: importance questions (0 to 100)") +
  theme(axis.text=element_text(size=11), 
        legend.text=element_text(size=9), strip.background =element_rect(fill="lightgrey"), 
        strip.text = element_text(colour = 'black'),
        strip.text.x = element_text(size = 12)) 

plot1







####### Mechanism outcomes
## Thinking has your position, liking the party
data$diffparta_partyb_pos <- (data$data_outcome_pa_3) - (data$data_outcome_pb_3)
data$diffparta_partyb_like <- (data$data_outcome_pa_4) - (data$data_outcome_pb_4)

mod <- (lm(diffparta_partyb_pos~treatment_data  + pos_data + Gender + edu_cont, data=data))
summary(mod)
mod1 <- (lm(data_outcome_pa_3~treatment_data  + pos_data + Gender + edu_cont, data=data))
summary(mod1)
mod2 <- (lm(data_outcome_pb_3~treatment_data  + pos_data + Gender + edu_cont, data=data))
summary(mod2)


mod3 <- (lm(diffparta_partyb_like~treatment_data  + pos_data + Gender + edu_cont, data=data))
summary(mod3)
mod4 <- (lm(data_outcome_pa_4~treatment_data  + pos_data + Gender + edu_cont, data=data))
summary(mod4)
mod5 <- (lm(data_outcome_pb_4~treatment_data  + pos_data + Gender + edu_cont, data=data))
summary(mod5)




## Create a table for in the appendix
table <- texreg(l=list(mod, mod1, mod2, mod3, mod4, mod5),
                use.packages=F,
                stars=c(0.10, 0.05, 0.01), 
                digits=3, include.ci = FALSE, booktabs=T,
                custom.model.names = c("Diff in position", "Position Agree Party", "Position Ambiguity Party", "Diff in liking", "Like agree statement", "Like ambiguity statement"),
                caption.above=T,
                label="exp2_mech",
                caption="Effect of salience treatment on ability to distinguish between parties",
                threeparttable = TRUE,
                float.pos="hptb!",
                custom.coef.names = c("Intercept", "Treated", "Position Disagree", "Female", "Education"),
                reorder.coef = c(2,3,4,5,1),
                custom.note="\\item %stars. The difference models use as outcome whether people differentiate between the party that agrees with them and the party that is ambiguous on a question that asks 'does the party have your position' and whether respondents 'like the statement by the party'. Both are composite scales that are composed of two continuous items (from 0 to 100). The composites range from -100 (completely preferring the ambiguous party) to 100 (completely preferring the agreeing party). The other models focus directly on these questions for each party. Models are estimated through OLS")

#Table A7
#print(table, file = "C:/Users/jonne/Dropbox/Apps/Overleaf/Too Important to Ignore v11 - resub JOP/tables/exp2_mech.tex")
print(table, file = "tables/exp2_mech.tex")



# Create a plot with the mechanism outcomes
resmod <- data.frame(coef=c(summary(mod)$coefficients[2], summary(mod3)$coefficients[2]),
                     SEs=c(summary(mod)$coefficients[7], summary(mod3)$coefficients[7]),
                     outcome=c("Party has your position", "Liking the statement"))



resmod$conf_high <- resmod$coef + 1.96 * resmod$SEs
resmod$conf_low <- resmod$coef - 1.96 * resmod$SEs
resmod$conf_high10 <- resmod$coef + 1.282 * resmod$SEs
resmod$conf_low10 <- resmod$coef - 1.282 * resmod$SEs



plot2 <- ggplot(resmod) + 
  geom_pointrange(aes(x=coef, y=outcome, xmin=conf_low, xmax=conf_high), size=0.9) +
  geom_linerange(aes(x=coef, y=outcome, xmin=conf_low10, xmax=conf_high10), size=1.8) +
  geom_vline(xintercept = 0, linetype="dashed") +
  labs(y="", x="Treatment effect") + 
  facet_grid(~"DVs: mechanism questions (-100 to 100)") +
  theme(axis.text=element_text(size=11), 
        legend.text=element_text(size=9), strip.background =element_rect(fill="lightgrey"), 
        strip.text = element_text(colour = 'black'),
        strip.text.x = element_text(size = 12)) 

plot2



# Add plots together and save
plots <- ggarrange(plot1, plot2, nrow=2, ncol=1)

plots

# Save the plots (Figure 8)
ggsave(plot=plots, "plots/fig8_exp2_mechmani.png", dpi = 300, width=7.1, height=4.2)
ggsave(plot=plots, "plots/fig8_exp2_mechmani.tiff", dpi = 300, width=7.1, height=4.2, device="tiff")
#ggsave(plot=plots, "C:/Users/jonne/Dropbox/Apps/Overleaf/Too Important to Ignore v11 - resub JOP/plots/exp2_mechmani.png", dpi = 300, width=7.1, height=4.2)







########## Show that position change does not influence the projection outcome
data$diffparta_partyb_pos <- (data$data_outcome_pa_3) - (data$data_outcome_pb_3)

mod <- (lm(diffparta_partyb_pos~treatment_data  + pos_data + Gender + edu_cont, data=data))
mod1 <- (lm(diffparta_partyb_pos~treatment_data  + pos_data + Gender + edu_cont + data_imp_10, data=data))



# Create a table for in the appendix
table <- texreg(l=list(mod, mod1),
                use.packages=F,
                stars=c(0.10, 0.05, 0.01), 
                digits=3, include.ci = FALSE, booktabs=T,
                custom.model.names = c("No position controls", "Position controls"),
                caption.above=T,
                label="exp2_postcont",
                caption="Effect of salience treatment on thinking the party has your position",
                threeparttable = TRUE,
                float.pos="htpb!",
                custom.coef.names = c("Intercept", "Treated", "Position Disagree", "Female", "Education", "Position"),
                reorder.coef = c(2,3,4,5,6,1),
                custom.note="\\item %stars. Both models use as outcome the difference between the party that agrees with a respondent and the party that is ambiguous on a question that asks if people think the party has their position (from 0 to 100). The difference scale ranges from -100 (completely thinking the ambiguous party has your position) to 100 (completely thinking the agreeing party has your position). 'Position' is measured using a 0-100 agree vs disagree scale that asks people if they think more data centers should be build. Models are estimated through OLS")

#Table A6
#print(table, file = "C:/Users/jonne/Dropbox/Apps/Overleaf/Too Important to Ignore v11 - resub JOP/tables/exp2_postcont.tex")
print(table, file = "tables/exp2_postcont.tex")











################### Updating experiment: show that the treatment did not increase importance nor outcome -------------------
## Main two outcomes and manipulation checks in a single table

# binary outcome
data$wage_outcome_binar <- ifelse(data$wage_binary=="Partij A", 1, 0)
mod <- (lm(wage_outcome_binar~treatment_wage +  Gender + edu_cont, data=data))


# continuous outcome
data$wage_diffparta_partyb <- ((data$wage_partya_1 +data$wage_partya_2)/2) - 
  ((data$wage_partyb_1 + data$wage_partyb_2)/2)
mod1 <- (lm(wage_diffparta_partyb~treatment_wage +  Gender + edu_cont, data=data))


# updating outcome
mod2 <- (lm(wage_updating_5~treatment_wage +  Gender + edu_cont, data=data))


# importance outcomes
data$wage_import_comp <- (data$wage_imp_1 + data$wage_imp_9)/2
mod3 <- (lm(wage_imp_1~treatment_wage +  Gender + edu_cont, data=data))
mod4 <- (lm(wage_imp_9~treatment_wage +  Gender + edu_cont, data=data))
mod5 <- (lm(wage_import_comp~treatment_wage +  Gender + edu_cont, data=data))


## Create a table for in the appendix
table <- texreg(l=list(mod, mod1, mod2, mod3, mod4, mod5),
                use.packages=F,
                stars=c(0.05, 0.01, 0.001), 
                digits=3, include.ci = FALSE, booktabs=T,
                custom.model.names = c("Choosing P", "Diff between P", "Updating","Imp subject", "More Imp", "Imp comp"),
                caption.above=T,
                label="exp2_wage_all",
                caption="Effects of the alternative updating salience treatment",
                threeparttable = TRUE,
                float.pos="htpb!",
                custom.coef.names = c("Intercept", "Treated", "Female", "Education"),
                reorder.coef = c(2,3,4,1),
                custom.note="\\item %stars. Choose P is the binary outcome capturing whether people prefer the clear party. Diff between P captures the difference between both parties on the composite variables. Updating captures whether people update their priors after the treatment. The first two importance questions capture the individual scales and the final question is the composite of both. Models are estimated through OLS")

# Table A9
#print(table, file = "C:/Users/jonne/Dropbox/Apps/Overleaf/Too Important to Ignore v11 - resub JOP/tables/exp2_wage_all.tex")
print(table, file = "tables/exp2_wage_all.tex")


















